﻿using mediaControl.Utils;
using mediaControl.Vo;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace mediaControl.ViewModels
{
    public class VideoSettingViewModel : BindableBase, IDialogAware
    {
        public string Title { get; set; }
        public ObservableCollection<ScreenItemVo> ScreenItemVos { get { return screenItemVos; } set { screenItemVos = value; RaisePropertyChanged(); } }

        private ObservableCollection<ScreenItemVo> screenItemVos = new ObservableCollection<ScreenItemVo>();

        //事件聚合器
        private readonly IEventAggregator eventAggregator;

        private ProgramItem programItem;

        private ProgramVo selectedItem;

        //项目类型 1 视频，2图片，3文件夹，4网页内容，5采集卡，6电脑桌面，7黑场颜色
        private String itemType;

        //项目路径
        private String itemPath;

        //显示模式
        private String displayMode;

        //播放模式
        private String playMode;

        private List<String> screens;

        private bool isLink = false;

        private int residenceTime;
        //是否静音
        private bool isMute;

        public DelegateCommand GetFilePathCommand { get; set; }

        public DelegateCommand ConfirmCommand { get; set; }

        public VideoSettingViewModel(IEventAggregator eventAggregator)
        {
            this.eventAggregator = eventAggregator;
            GetFilePathCommand = new DelegateCommand(getFilePath);
            ConfirmCommand = new DelegateCommand(confirm);
            initData();
        }

        private void confirm()
        {
            if (String.IsNullOrEmpty(ItemPath)) {
                MessageBox.Show("请选择视频！");
                return;
            }
            string programFilePath = SelectedItem.FilePath + "\\" + SelectedItem.Name + ".info";
            string content = "";//返回的结果字符串
            using (StreamReader sr = new StreamReader(programFilePath))
            {
                content = sr.ReadToEnd();//一次性把文本内容读完
            }
            JObject programInfo = JsonConvert.DeserializeObject<JObject>(content);
            List<ProgramItem> programItems = new List<ProgramItem>();
            if (programInfo.ContainsKey("programItems"))
            {
                programItems = JsonConvert.DeserializeObject<List<ProgramItem>>((String)programInfo.GetValue("programItems"));
            }
            foreach (ProgramItem programItem1 in programItems)
            {
                if (programItem1.ItemType.Equals("1") && programItem1.ItemPath.Equals(ItemPath))
                {
                    MessageBox.Show("视频已存在！");
                    return;
                }
            }
            List<String> selectedScreens = new List<String>();
            //处理屏幕
            foreach (ScreenItemVo screenItem in ScreenItemVos)
            {
                if (screenItem.IsSelected)
                {
                    selectedScreens.Add(screenItem.Id + "");
                }
            }
            ProgramItem programItem = new ProgramItem("1","1", ItemPath);
            programItems.Add(programItem);
            if (programInfo.ContainsKey("programItems"))
            {
                programInfo["programItems"] = JsonConvert.SerializeObject(programItems);
            }
            else
            {
                programInfo.Add("programItems", JsonConvert.SerializeObject(programItems));
            }
            WriteFileUtil.writeFile(programFilePath, JsonConvert.SerializeObject(programInfo), "2");
            //通知列表显示
            IDialogParameters parameters = new DialogParameters();
            parameters.Add("programItems", programItems);
            RequestClose?.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.OK, parameters));
        }

        private void getFilePath()
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Title = "选择视频";
            openFileDialog.Filter = "视频文件|*.mp4; *.avi; *.wmv; *.mov";
            openFileDialog.FilterIndex = 1;
            if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
               ItemPath = openFileDialog.FileName; 
            }
        }

        public event Action<IDialogResult> RequestClose;

        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {
            
        }

        public void OnDialogOpened(IDialogParameters parameters)
        {
           this.Title = "设置视频";
            ProgramItem = parameters.GetValue<ProgramItem>("programItem");
            SelectedItem = parameters.GetValue<ProgramVo>("program");
            //屏幕选项
            ObservableCollection<ScreenVo> screenVos = WriteFileUtil.getScreenItemList();
            ScreenItemVos.Clear();
            foreach (ScreenVo screen in screenVos) {
                bool isSelected = false;
                if (null != programItem)
                {
                    if (ProgramItem.Screens.Contains(screen.Id + ""))
                    {
                        isSelected = true;
                    }
                }
                ScreenItemVo screenItemVo = new ScreenItemVo(screen.Id,screen.ScreenName,isSelected);
                ScreenItemVos.Add(screenItemVo);
            }
        }

        public DataTable DisplayModeTabe { get; set; }

        public DataTable PlayModeTabe { get; set; }
        public ProgramItem ProgramItem { get => programItem; set => programItem = value; }
        public ProgramVo SelectedItem { get => selectedItem; set => selectedItem = value; }
        public string ItemType { get { return itemType; } set { this.itemType = value; RaisePropertyChanged(); } }
        public string ItemPath { get { return itemPath; } set { this.itemPath = value; RaisePropertyChanged(); } }
        public string DisplayMode { get { return displayMode; } set { displayMode = value; RaisePropertyChanged(); } }
        public string PlayMode { get { return playMode; } set { this.playMode = value; RaisePropertyChanged(); } }
        public List<string> Screens { get => screens; set => screens = value; }
        public bool IsLink { get
            {
                return isLink;
            }
            set
            {
                isLink = value;
                RaisePropertyChanged();
            } }
        public int ResidenceTime { get
            {
                return residenceTime;
            }
            set
            {
                residenceTime = value;
                RaisePropertyChanged();
            } }
        public bool IsMute { get { return isMute; } set { this.isMute = value; RaisePropertyChanged(); } }

        public void initData() {
            DisplayModeTabe = new DataTable();
            DisplayModeTabe.Columns.Add("状态值");
            DisplayModeTabe.Columns.Add("状态显示");
            DisplayModeTabe.Rows.Add("1", "满屏");
            DisplayModeTabe.Rows.Add("2", "缩放16:9");
            DisplayModeTabe.Rows.Add("3", "缩放4:3");

            PlayModeTabe = new DataTable();
            PlayModeTabe.Columns.Add("状态值");
            PlayModeTabe.Columns.Add("状态显示");
            PlayModeTabe.Rows.Add("1", "停止");
            PlayModeTabe.Rows.Add("2", "单曲循环");

        }
    }
}
